package com.neusoft.service.impl;

import com.neusoft.mapper.ClientWalletMapper;
import com.neusoft.mapper.CreditBillMapper;
import com.neusoft.service.PenaltyService;
import com.neusoft.domain.VirtualWalletPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PenaltyServiceImpl implements PenaltyService {
    private static final BigDecimal PENALTY_RATE = BigDecimal.valueOf(0.01);  // 1% penalty rate
    @Autowired
    private ClientWalletMapper walletClientMapper;

    @Autowired
    private CreditBillMapper creditBillMapper; // 引入 CreditBillMapper

    @Override
    public void applyPenalty(Long walletId, BigDecimal overdueAmount) {
        VirtualWalletPO walletPO = walletClientMapper.findById(walletId);
        walletPO.setPenaltyAmount(overdueAmount.multiply(PENALTY_RATE).add(walletPO.getPenaltyAmount()));
        // 记录滞纳金到数据库
        walletClientMapper.updatePenalty(walletId, walletPO.getPenaltyAmount()); // 假设您已在 walletClientMapper 中定义了此方法
    }

    @Override
    public BigDecimal calculatePenalty(Long walletId) {
        BigDecimal totalPenalty = BigDecimal.ZERO;

        // 查询所有未偿还账单
        List<BigDecimal> overdueBills = creditBillMapper.findOutstandingBills(walletId); // 假设这个方法返回所有未偿还账单的金额

        // 计算每个逾期账单的滞纳金
        for (BigDecimal overdueAmount : overdueBills) {
            totalPenalty = totalPenalty.add(overdueAmount.multiply(PENALTY_RATE));
        }

        return totalPenalty;
    }
}


